package com.ljkj.cordial.chat.fragment;


import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.activity.FriendInfoActivity;
import com.ljkj.cordial.chat.activity.SearchFriendActivity;
import com.ljkj.cordial.chat.adapter.NavContactAdapter;
import com.ljkj.cordial.chat.base.BaseFragment;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.util.ObserverThread;
import com.nic.view.util.JLog;
import com.nic.view.util.contactList.ContactRecycleView;
import com.nic.view.widget.ContainsEmojiEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class NavContactFragment extends BaseFragment implements NavContactAdapter.OnContactItemClickListener {


    //    @BindView(R.id.listView)
//    PinyinListView listView;
    @BindView(R.id.iv_empty)
    ImageView ivEmpty;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.et_input)
    ContainsEmojiEditText etInput;
    @BindView(R.id.ll_add_friend)
    LinearLayout llAddFriend;
    @BindView(R.id.recycler)
    ContactRecycleView recycler;

    //    private PersonnelAdapter personnelAdapter;
    private NavContactAdapter navContactAdapter;
    Unbinder unbinder;
    private int selectedPosition;

    public NavContactFragment() {
        // Required empty public constructor
    }


    @Override
    public int getContentLayoutId() {
        return R.layout.fragment_nav_contact;
    }

    @Override
    public void initView(View rootView) {
        unbinder = ButterKnife.bind(this, rootView);
        initAdapter();
//        listView.setBarSelectionListener(new PinyinListView.onSideBarSelectionListener() {
//            @Override
//            public void onSelected(String selectStr) {
//                for (int i = 0; i < personnelAdapter.getList().size(); i++) {
//                    FriendTable bean = personnelAdapter.getList().get(i);
//                    if (bean.getFirstLetter().equals(selectStr) && TextUtils.isEmpty(bean.getPinyin())) {
//                        listView.getListView().setSelection(i);
//                    }
//                }
//            }
//        });
//        listView.getListView().setEmptyView(ivEmpty);
        recycler.setBarSelectionListener(new ContactRecycleView.onSideBarSelectionListener() {
            @Override
            public void onSelected(String selectStr) {
                for (int i = 0; i < navContactAdapter.getList().size(); i++) {
                    FriendTable bean = navContactAdapter.getList().get(i);
                    if (bean.getFirstLetter().equals(selectStr) && TextUtils.isEmpty(bean.getPinyin())) {
                        recycler.getListView().scrollToPosition(i);
                    }
                }
            }
        });
        llAddFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchFriendActivity.jumpTo(activity, Canstants.SEARCH_TYPE_FRIEND);
            }
        });

    }

    private void initAdapter() {
//        personnelAdapter = new PersonnelAdapter(CheckType.NONE);
//        personnelAdapter.setOnItemClickListener(new PersonnelAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, Object obj, int position) {
//                selectedPosition = position;
//                if (CheckType.NONE.equals(personnelAdapter.getCheckType())) {
//                    FriendTable item = (FriendTable) obj;
//                    FriendInfoActivity.jumpTo(view.getContext(), IMSContacts.UserType.REGISTERED_USER.equals(item.getUserType()) ? Canstants.KEY_REGISTER : Canstants.KEY_VISITOR, item.getFriendNickname(), item.getFriendUserId(), item.getUserType());
//                }
//            }
//        });
//
//        listView.getListView().setAdapter(personnelAdapter);
        navContactAdapter = new NavContactAdapter();
        navContactAdapter.setContactItemClickListener(this);
        recycler.getListView().setAdapter(navContactAdapter);
        recycler.getListView().addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                   selectedPosition = recycler.findFirstVisibleItemPosition();
                }

            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (!recyclerView.canScrollVertically(-1)) {
                    //top
//                    limit = mLinearLayoutManager.getChildCount();
                } else if (!recyclerView.canScrollVertically(1)) {
                    //bottom
                } else if (dy < 0) {
                    //up
                } else if (dy > 0) {
                    //bottom
                }
            }
        });
    }


    public void setData(List<FriendTable> userInfors) {
        ContactRecycleView.insertTitle(userInfors, new ContactRecycleView.PinyinFactory<FriendTable>() {

            @Override
            public FriendTable create() {
                return new FriendTable();
            }

            @Override
            public void finish(final List<FriendTable> t) {
                if (activity != null && !activity.isFinishing()) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
//                            personnelAdapter.setList(t);
                            ivEmpty.setVisibility(View.GONE);
                            navContactAdapter.setList(t);
//                            recycler.moveToPosition(selectedPosition);
                        }
                    });
                }


            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        queryOfflineData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.iv_search)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etInput.getText())) {
            return;
        }
        ObserverThread.create().onNextThread0(new ObserverThread.OnActionListener<List<FriendTable>>() {
            @Override
            public void onAction(List<FriendTable> list) {
                search(list);
            }

            @Override
            public List<FriendTable> onThreadAction() {
                return FriendTable.query(etInput.getText().toString());
            }
        });
    }

    public void queryOfflineData() {
        ObserverThread.create().onNextThread0(new ObserverThread.OnActionListener<List<FriendTable>>() {
            @Override
            public void onAction(List<FriendTable> list) {
                search(list);
            }

            @Override
            public List<FriendTable> onThreadAction() {
                return FriendTable.query();
            }
        });

    }

    private void search(List<FriendTable> list) {
        if (list != null && !list.isEmpty()) {
//            initAdapter();
            setData(list);
            ivEmpty.setVisibility(View.GONE);
        } else {
//            personnelAdapter.clear();
            navContactAdapter.clear();
            ivEmpty.setVisibility(View.VISIBLE);
        }

    }

//    public void selection(int position) {
//        listView.getListView().post(new Runnable() {
//            @Override
//            public void run() {
//                listView.getListView().smoothScrollToPosition(position);
//            }
//        });
//    }

    @Override
    public void onItemClick(View view, FriendTable item, int position) {
        selectedPosition = position;
        FriendInfoActivity.jumpTo(view.getContext(), IMSContacts.UserType.REGISTERED_USER.equals(item.getUserType()) ? Canstants.KEY_REGISTER : Canstants.KEY_VISITOR, item.getFriendNickname(), item.getFriendUserId(), item.getUserType());
    }
}
